package com.zhuqieli.database_learn.config;


import com.zhuqieli.database_learn.filter.MyFilter;
import com.zhuqieli.database_learn.interceptor.IpInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.DispatcherType;
import javax.servlet.Filter;

/**
 * @author dupr
 * @description 配置类
 * @date 2023/8/30 16:53:43
 */

@Configuration
public class MyConfig implements WebMvcConfigurer {
    @Autowired
    private IpInterceptor ipInterceptor;

    @Autowired
    private MyFilter myFilter;

    /**
     * 配置拦截器路径
     *
     * @author dupr
     * @date 2023/8/30 16:53:59
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(ipInterceptor)
                .addPathPatterns("/**");
    }

    /**
     * 配置过滤器路径
     *
     * @author dupr
     * @date 2023/8/30 16:54:24
     */
    @Bean
    public FilterRegistrationBean<Filter> myFilterConfigRegistration() {
        String url = "/test/study";
        FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<Filter>();
        registration.setDispatcherTypes(DispatcherType.REQUEST);
        registration.setFilter(myFilter);
        registration.addUrlPatterns(url);
        registration.setName("myFilter");
        registration.setEnabled(true);
        registration.addInitParameter("enabel", "true");
        return registration;
    }
}
